#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then restore a subdirectory
# to see if some extra directories are restored also.
#
# On Windows, it creates a problem when using RegexWhere
#
# RegexWhere: !F:/priv/RZB1_Swiadectwa!!i,!^!F:/priv/RESTORE!
# Bacula is trying to restore F:/priv/ and the regex engine is
# creating a directory "F:/priv/RESTOREF:/priv/" which is not possible
#
TestName="bug-rwhere-7768"
JobName=bug-rwhere-7768
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
echo $cwd/build >${tmp}/file-list

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData", "No", "Job")'
sed -i 's/!restored/restored/i' $conf/bacula-fd.conf

change_jobname NightlySave $JobName
start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log1.out
label storage=File volume=TestVolume000
run job=$JobName level=full yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

cat <<END_OF_DATA >${tmp}/bconcmds
@# 
@# now do a restore that will fail (JobId 2)
@#
@$out ${tmp}/log2.out
setdebug level=10 storage=File
restore strip_prefix="$cwd/build" add_prefix="$cwd/tmp" where=/ storage=File
5
cd $cwd/build
m po
done
yes
wait
messages
@$out $tmp/log3.out
list joblog jobid=2
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_bacula

nb=`grep $tmp $tmp/log3.out | grep -v po | wc -l`
if [ $nb -gt 0 ]; then
    print_debug "ERROR: Should not find a line with $tmp in $tmp/log3.out, only po directory was selected"
    estat=1
fi

check_two_logs
end_test
